using Tetris.Game.Common;
using Tetris.Game.Common.TetrisCenter;
using Tetris.Game.ControlsBase;
using Tetris.Game.ControlsBase.Common;

namespace Tetris.Game
{
    public partial class FrmMain : Form
    {
        private readonly FrmPlay _frmPlay;
        private readonly FrmRight _frmRight;
        private readonly TetrisGameCenter _tetrisGameCenter;
        public FrmMain()
        {
            InitializeComponent();

            _tetrisGameCenter = new TetrisGameCenter();
            _frmPlay = new FrmPlay();
            _frmRight = new FrmRight(_tetrisGameCenter);


            AddForm(plGame, _frmPlay);

            AddForm(plRight, _frmRight);

            _tetrisGameCenter.InitData = _frmPlay.InitData;
            _tetrisGameCenter.PaintBlockControlCallback = _frmPlay.PaintBlockControl;
            _tetrisGameCenter.UpdateLocationCallback = _frmPlay.UpdateLocation;
            _tetrisGameCenter.ArrangeBlockCallback = _frmPlay.ArrangeBlock;
            _tetrisGameCenter.CountDownStartCallback = CountDownStart;

            _tetrisGameCenter.NextTetrisShapeCallback = _frmRight.NextTetrisShape;
            _tetrisGameCenter.GameOverCallback = _frmRight.StopGame;
            _tetrisGameCenter.GameResultCallback = _frmRight.GameResult;

            _frmPlay.MoveCallback = _tetrisGameCenter.Move;
            _frmPlay.KeystrokeCallback = Keystroke;

            this.countDownControl1.CountDownEndCallback = CountDownEnd;
            this.countDownControl1.Visible = false;

            kcUp.Click += Keystroke_Click;
            kcDown.Click += Keystroke_Click;
            kcLeft.Click += Keystroke_Click;
            kcRight.Click += Keystroke_Click;
            kcSpace.Click += Keystroke_Click;
        }
        private void Keystroke_Click(object sender, EventArgs e)
        {
            KeystrokeControl keystroke = sender as KeystrokeControl;
            _tetrisGameCenter.Move(keystroke.Keystroke switch
            {
                KeystrokeType.Up => DirectionType.Up,
                KeystrokeType.Down => DirectionType.Down,
                KeystrokeType.Left => DirectionType.Left,
                KeystrokeType.Right => DirectionType.Right,
                _ => DirectionType.Space
            });
        }
        public void Keystroke(KeystrokeType keystrokeType)
        {
            switch (keystrokeType)
            {
                case KeystrokeType.Up:
                    kcUp.KeystrokeClick();
                    break;
                case KeystrokeType.Left:
                    kcLeft.KeystrokeClick();
                    break;
                case KeystrokeType.Right:
                    kcRight.KeystrokeClick();
                    break;
                case KeystrokeType.Down:
                    kcDown.KeystrokeClick();
                    break;
                case KeystrokeType.Space:
                    kcSpace.KeystrokeClick();
                    break;
            }
        }
        private void CountDownStart()
        {
            this.countDownControl1.Start();
        }
        private void CountDownEnd()
        {
            _tetrisGameCenter.CountDownEnd();
        }
        private void AddForm(Control parent, Form form)
        {
            form.TopLevel = false;
            form.Dock = DockStyle.Fill;
            parent.Controls.Add(form);
            form.Show();
        }
        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            _tetrisGameCenter.GameStatus = Common.TetrisCenter.TetrisBase.GameStatus.Stop;
            _frmPlay.Dispose();
            _frmRight.Dispose();
            Thread.Sleep(50);
            _tetrisGameCenter.Dispose();
            this.Dispose();
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }
    }
}